<?php

namespace rongyun\components;

use Yii;
use yii\base\Exception;
use yii\helpers\Json;

class BaseTransport extends \yii\base\Object {

    private $client = null;
    private $_error = null;

    public function __construct($client, $config = array()) {
        $this->client = $client;
        parent::__construct($config);
    }

    public function getClient() {
        return $this->client;
    }

    public function http($action, $params, $contentType = 'urlencoded', $module = 'im', $httpMethod = 'POST') {
        $response = $this->client->curl($action, $params, $contentType, $module, $httpMethod);
        if (empty($response)) {
            Yii::error('bad request');
            return false;
        }
        return Json::decode($response);
    }

    public function getErrorMessage() {
        return $this->_error;
    }

    public function setErrorMessage($message) {
        $this->_error = $message;
    }

    public static function consoleException($message, $args = []) {
        Yii::error([
            'message' => $message,
            'args' => $args,
        ], __METHOD__);
        return false;
    }

}
